/*
 * To change this pack, choose Tools | Templates
 * and open the pack in the editor.
 */
package com.jimu.longkong.web.controller;

import com.jimu.longkong.web.Web;
import com.jimu.longkong.db.cache.Cache;
import com.jimu.longkong.db.domain.User;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author mazhiqiang
 */
@Controller
//@SessionAttributes("currUser")
public class UserController {

    @RequestMapping(value = "/user/signup", method = RequestMethod.POST)
    public ModelAndView singnup(@ModelAttribute("User") User user, BindingResult result) {
        if (user == null) {
            return Web.errpage("错误", "用户已登录，不能注册！");
        }
        System.err.println("----------->" + user.getName());
        // 判断是否已经登陆
        HttpSession session = Web.getSession();
        User u = (User) session.getAttribute("user");
        if (u != null) { // 判断重复登录
            return Web.errpage("错误", "用户已登录，不能注册！");
        }
        
        // 判断用户是否存在
        u = Cache.user.getBytheName(user.getName());
        if (u != null) {
            return new ModelAndView("/user/signup.html")
                    .addObject("user", user)
                    .addObject("message", "注册错误，用户已经存在");
        } else {
            if (Cache.user.insert(user)) {
                // 直接登陆
                session.setAttribute("user", user);
                return Web.redirect("/");
            } else {
                return new ModelAndView("/user/signup.html")
                        .addObject("user", user)
                        .addObject("message", "注册失败");
            }
        }
    }

    @RequestMapping(value = "/user/signup", method = RequestMethod.GET)
    public ModelAndView singnupForm(ModelMap model) {
        return new ModelAndView("/user/signup.html");
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public ModelAndView login(@ModelAttribute("User") User user, BindingResult result, ModelMap model) {
        HttpSession session = Web.getSession();
        User u = (User) session.getAttribute("user");
        if (u != null) { // 判断重复登录，直接退出
            session.setAttribute("user", null);
            return Web.redirect("/");
        }
        u = Cache.user.getBytheNamePassword(user.getName(), user.getPassword());
        if (u != null) {
            Web.getSession().setAttribute("user", u);
            return Web.redirect("/");
        } else {
            model.addAttribute("message", "用户名密码错误，无法登陆。");
            model.addAttribute("user", user);
            return new ModelAndView("/user/login.html");
        }
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.GET)
    public ModelAndView loginForm(ModelMap model) {

//        model.addAttribute("message", message);
        return new ModelAndView("/user/login.html");
    }

    @RequestMapping(value = "user/logout", method = RequestMethod.GET)
    public ModelAndView logout(ModelMap model) {
        HttpSession session = Web.getSession();
        session.setAttribute("user", null);

        return Web.redirect("/");
    }
}